package com.zwh.autoconfigure;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import com.zwh.autoconfigure.eventbus.EventBusCenter;
import com.zwh.autoconfigure.eventbus.EventBusListener;
import com.zwh.common.spring.SpringContextUtils;

/**
 * @author monkey
 * @date 2020/11/10 17:58
 **/
@Component
public class MyApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        EventBusCenter eventBusCenter = SpringContextUtils.getBean(EventBusCenter.class);
        // 获取所有带有 @EventBusListener 的 bean，将他们注册为监听者
        Map<String, Object> beansWithAnnotation = SpringContextUtils.getApplicationContext().getBeansWithAnnotation(EventBusListener.class);
        List<Object> listeners = beansWithAnnotation.entrySet().stream().map(Map.Entry::getValue).collect(Collectors.toList());
        eventBusCenter.registry(listeners);
    }
}
